<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" label-width="77px" class="ele-form-search" @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="所属考试:">
              <el-select v-model="where.exam_id" clearable filterable placeholder="请选择所属考试">
                <el-option v-for="item in examOption" :key="item.id" :label="item.title" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="考生信息:">
              <el-input clearable v-model="where.userinfo" placeholder="考生信息如：身份证、准考证号、姓名" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询
              </el-button>

              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <!-- <el-button @click="removeBatch" type="danger" icon="el-icon-delete" class="ele-btn-icon" size="small"
            v-if="permission.includes('sys:examlog:dall')">批量删除
          </el-button> -->
          <el-button @click="handleExport()" type="primary" icon="el-icon-up" class="el-icon-download" size="small"
            v-if="permission.includes('sys:examlog:export')">导出成绩
          </el-button>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch v-model="row.status" @change="editStatus(row)" :active-value="1" :inactive-value="2" />
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <!-- <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:examlog:edit')">修改
          </el-link> -->
          <el-popconfirm class="ele-action" title="确定要删除此考生成绩吗？" @confirm="remove(row)">
            <el-link type="danger" slot="reference" :underline="false" icon="el-icon-delete"
              v-if="permission.includes('sys:examlog:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <examlog-edit :data="current" :visible.sync="showEdit" @done="reload" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ExamlogEdit from './examlog-edit';
export default {
  name: 'Examlog',
  components: { ExamlogEdit },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/examlog/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: '编号',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'exam_title',
          label: '所属考试',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 160,
        },
        {
          prop: 'realname',
          label: '考生姓名',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 60,
        },
        {
          prop: 'examinee_number',
          label: '准考证号',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 60,
        },
        {
          prop: 'usetime',
          label: '考试用时(分钟)',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 80,
        },
        {
          prop: 'num',
          label: '题目总数',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 60,
        },
        {
          prop: 'answer_num',
          label: '答题数量',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 60,
        },
        {
          prop: 'unanswer_num',
          label: '未答数量',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 60,
        },
        {
          prop: 'score',
          label: '考试成绩',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: 'create_time',
          label: '交卷时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 120,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'update_time',
          label: '更新时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 120,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 130,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      examOption: [],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false
    };
  },
  // 页面创建时
  created() {
    this.examList();
  },
  methods: {

    // 上传到本地
    handleExport() {
      const loading = this.$loading({
        lock: true,
        text: "下载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.$http.get('/examlog/exportExamlog',{params: this.where}).then(res => {
        let list = res.data.data;
        console.log(list)
        import("@/vendor/Export2Excel").then((excel) => {
          const tHeader = [
            '所属考试',
            '考生姓名',
            '准考证号',
            '考试用时',
            '题目总数',
            '答题数量',
            '未答数量',
            '考试成绩',
            '交卷时间',
          ];
          let data = list.map((item) => {
            return [
              item.exam_title,
              item.realname,
              item.examinee_number,
              item.usetime,
              item.num,
              item.answer_num,
              item.unanswer_num,
              item.score,
              item.create_time
            ];
          });
          excel.export_json_to_excel({
            header: tHeader, //表头 必填
            data, //具体数据 必填
            filename: "考试成绩信息表", //非必填
            autoWidth: true, //非必填
            bookType: "xlsx", //非必填
          });
          loading.close();
        });
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 获取考试列表 */
    examList() {
      this.$http.get('/exam/getExamSelectList').then(res => {
        if (res.data.code === 0) {
          this.examOption = res.data.data;
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http.post('/examlog/delete', { id: row.id }).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据')
        return;
      }
      this.$confirm('确定要删除选中的考生成绩吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({ lock: true });
        this.$http.post('/examlog/delete', { id: this.selection.map(d => d.id) }).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({ type: 'success', message: res.data.msg });
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        "id": row.id,
        "status": row.status
      })
      this.$http.post('/examlog/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({ type: 'success', message: res.data.msg });
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped></style>
